package br.com.comanda;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import br.com.comanda.customadapters.ItemAdapter;
import br.com.comanda.service.ItemService;
import br.com.comanda.vos.Item;

public class EditItemActivity extends Activity {

	private ItemService itemService;

	private ListView itemListView;

	private List<Item> itemList = new ArrayList<Item>();

	private Integer accountId;

	private ArrayAdapter<Item> itemAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.edit_item);

		init();
	}

	private void init() {
		this.itemService = new ItemService();
		this.accountId = Integer.valueOf(getIntent().getExtras().get("accountId").toString());

		Log.i("ACCOUNT ID", this.accountId.toString());

		this.itemList = this.itemService.getItemsByAccount(this.accountId);

		this.itemAdapter = new ItemAdapter(this, R.layout.item_list, this.itemList);

		this.itemListView = (ListView) findViewById(R.id.lstItens);
		this.itemListView.setAdapter(this.itemAdapter);

		this.itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View childView, int position, long id) {
				Item item = EditItemActivity.this.itemList.get(position);
				showEditItemDialog(item);
			}
		});
	}

	public void showEditItemDialog(final Item item) {
		final Dialog dialog = new Dialog(this);
		dialog.getWindow();
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.edit_account_item);

		TextView dialogTitle = (TextView) dialog.findViewById(R.id.edititem_dialog_title);
		final EditText editText = (EditText) dialog.findViewById(R.id.quantidadeItemInput);
		Button btnAtualizar = (Button) dialog.findViewById(R.id.bt_atualizar);
		Button btnExcluir = (Button) dialog.findViewById(R.id.bt_excluir);

		dialogTitle.setText(item.getProduct().getName());
		editText.setText(item.getQuantity().toString());

		btnAtualizar.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Integer qtde = Integer.valueOf(editText.getText().toString());

				Item itemToUpdate = item;
				item.setQuantity(qtde);
				item.setTotal((double) (qtde * item.getProduct().getPrice()));
				EditItemActivity.this.itemService.updateItem(itemToUpdate);

				dialog.dismiss();
			}
		});

		btnExcluir.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				EditItemActivity.this.itemService.excluirItem(item.getId());
				dialog.dismiss();
				init();

			}
		});

		dialog.show();

	}

	public void refreshListView() {
		this.itemListView.refreshDrawableState();
	}
}
